﻿#include "appinit.h"
#include "qapplication.h"
#include "qdesktopwidget.h"
#include "qevent.h"
#include "qwidget.h"
#include "qdebug.h"

AppInit *AppInit::self = 0;
AppInit::AppInit(QObject *parent) : QObject(parent)
{
}

bool AppInit::eventFilter(QObject *obj, QEvent *evt)
{
    QWidget *w = (QWidget *)obj;
    if (!w->property("canMove").toBool()) {
        return QObject::eventFilter(obj, evt);
    }

    //存储桌面宽高以及全屏/左侧半屏/右侧半屏 区域
    static int deskWidth = qApp->desktop()->availableGeometry().width();
    static int deskHeight = qApp->desktop()->availableGeometry().height();
    static QRect fullRect = qApp->desktop()->availableGeometry();
    static QRect leftRect = QRect(0, 0, deskWidth / 2, deskHeight);
    static QRect rightRect = QRect(deskWidth / 2, 0, deskWidth / 2, deskHeight);
    bool autoRect = w->property("autoRect").toBool();

    static QPoint mousePoint;
    static bool mousePressed = false;

    QMouseEvent *event = static_cast<QMouseEvent *>(evt);
    if (event->type() == QEvent::MouseButtonPress) {
        if (event->button() == Qt::LeftButton) {
            mousePressed = true;
            mousePoint = event->globalPos() - w->pos();
            return true;
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        mousePressed = false;

        //计算全局坐标
        int x = event->globalPos().x();
        int y = event->globalPos().y();
        int offset = 10;

        //如果Y坐标在桌面顶部,则自动最大化
        //如果X坐标在桌面左侧,则自动左侧半屏幕
        //如果X坐标在桌面右侧,则自动右侧半屏幕
        //自动变化后记住当前窗体是自动产生的位置,以便下次恢复时自动应用变化前的位置
        if (!autoRect) {
            //存储最后一次的位置,自动矫正负数的坐标
            int oldX = w->geometry().x();
            oldX = oldX < 0 ? 0 : oldX;
            int oldY = w->geometry().y();
            oldY = oldY < 0 ? 0 : oldY;
            QRect oldRect = QRect(oldX, oldY, w->geometry().width(), w->geometry().height());

            if (y < offset) {
                w->setProperty("autoRect", true);
                w->setProperty("oldRect", oldRect);
                w->setGeometry(fullRect);
            } else if (x < offset) {
                w->setProperty("autoRect", true);
                w->setProperty("oldRect", oldRect);
                w->setGeometry(leftRect);
            } else if (x > (deskWidth - offset)) {
                w->setProperty("autoRect", true);
                w->setProperty("oldRect", oldRect);
                w->setGeometry(rightRect);
            }
        }

        return true;
    } else if (event->type() == QEvent::MouseMove) {
        if (mousePressed && (event->buttons() && Qt::LeftButton)) {
            if (!autoRect) {
                w->move(event->globalPos() - mousePoint);
            } else {
                QRect oldRect = w->property("oldRect").toRect();
                w->setProperty("autoRect", false);
                w->setGeometry(oldRect);
            }

            return true;
        }
    }

    return QObject::eventFilter(obj, evt);
}

void AppInit::start()
{
    qApp->installEventFilter(this);
}
